<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @inputOnSearch="inputOnSearch"
          @closeModal="closeModal"
          @orgsearch="orgsearch"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import {getLicenseByPid} from '@/views/app/biz/regisLicense/regisLicense'
import { getOrgInfoByPid } from '@/views/sys/org/org/org'
import { getInfoByPid, saveInfo } from './customer'

export default {
  components: {
    basicModal,
    basicForm,
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增品种',
        modalWidth: '1000px',
      },
      newOrg: true, 
      s_FormInfo: [
        { label: '请输入客户单位社会信用代码', isDisabled: false, name: 'keHushxxdm', type: 'orgsearch', placeholder: '' },
        { label: '客户编码', name: 'keHudm', isDisabled: false, type: 'input', placeholder: ' 客户编码' },
        // { label: '',name:'keHushxxdm',type:'input',placeholder:' 客户社会信用代码'},
        { label: '客户名称', name: 'keHumc', isDisabled: false, type: 'input', placeholder: ' 客户名称' },
        { label: '客户城市', name: 'chengShi', isDisabled: false, type: 'input', placeholder: ' 客户城市' },
        { label: '客户地点', name: 'diZhi', isDisabled: true, type: 'input', placeholder: ' 客户地点' },
        { label: '客户负责人姓名', name: 'fuZerxm', isDisabled: true, type: 'input', placeholder: ' 客户负责人姓名' },
        {
          label: '客户负责人联系电话',
          name: 'fuZerdh',
          isDisabled: true,
          type: 'input',
          placeholder: ' 客户负责人联系电话',
        },
        {
          label: '客户联系人姓名',
          name: 'lianXirxm',
          isDisabled: true,
          type: 'input',
          placeholder: ' 客户联系人姓名',
        },
        {
          label: '客户联系人联系电话',
          name: 'lianXirdh',
          isDisabled: true,
          type: 'input',
          placeholder: ' 客户联系人联系电话',
        },
      ],
      s_formDatas: {
        sysSeq: 0,
        pid: '',
        keHudm: null,
        keHuhy: null,
        keHushxxdm: null,
        keHuOrgPid: null,
        keHumc: null,
        chengShi: null,
        diZhi: null,
        fuZerxm: null,
        fuZerdh: null,
        lianXirxm: null,
        lianXirdh: null,
        zhuangTai: null,
      },
      s_rules: {
        keHushxxdm: [{ required: true, message: '信用代码必填', trigger: 'blur' }],
        keHumc: [{ required: true, message: '名称必填', trigger: 'blur' }],
        fuZerxm: [{ required: true, message: '负责人必填', trigger: 'change' }],
        lianXirxm: [{ required: true, message: '联系人必填', trigger: 'change' }],
        lianXirdh: [{ required: true, message: '联系人电话必填', trigger: 'blur' }],
      },
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        keHudm: null,
        keHuhy: null,
        keHushxxdm: null,
        keHumc: null,
        chengShi: null,
        diZhi: null,
        fuZerxm: null,
        fuZerdh: null,
        lianXirxm: null,
        lianXirdh: null,
        zhuangTai: null,
      }
      for (let item of this.s_FormInfo) {
        if (item.name === 'keHushxxdm') {
          item.isDisabled = false
        } else {
          item.isDisabled = true
        }
      }
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增客户'
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      for (let item of this.s_FormInfo) {   
        if (item.name === 'keHushxxdm'
           ||item.name === 'keHudm'
           ||item.name === 'keHumc'
           ||item.name === 'chengShi') {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改客户信息'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败，原因：'+res.message, duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      switch (name) {
        case 'zhuCezhPid':
          // console.log('aaaaaaaaaaaaaa',name)
          const zhucez = this.getZhuCezxx(val)
          break
        case 'keHuOrgPid':
          this.getOrgInfo(val)
          break
      }
    },
    // 获取注册证详细信息
    async getZhuCezxx(pid) {
      const res = await getLicenseByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezhPid: res.data.pid })
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezh: res.data.zhengZhaohm })
        this.s_formDatas = Object.assign(this.s_formDatas, { shengChanqymc: res.data.shengChancsmc })
        this.s_formDatas = Object.assign(this.s_formDatas, { pinMing: res.data.chanPinmc })
      }
    },
    // 初始化
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    },
    // 查询单位信息
    async getOrgInfo(val) {
      let res = await getOrgInfoByPid(val, {})
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas.keHumc = res.data.orgFullName
      }
      console.log(res)
    },
    // input搜索
    inputOnSearch(val, name) {
      console.log(val)
      console.log(name)
      switch (name) {
        case 'keHushxxdm':
          // console.log('aaaaaaaaaaaaaa',name)
          const zhucez = this.getZhuCezxx(val)
          break
        case 'keHuOrgPid':
          this.getOrgInfo(val)
          break
      }
    },
    // 单位搜索
    orgsearch(name, val) {
      console.log('val', val)
      console.log('name', name)
      this.s_formDatas.keHudm = val.orgIdno
      this.s_formDatas.keHushxxdm = val.orgIdno
      this.s_formDatas.keHuOrgPid = val.org.pid
      this.s_formDatas.keHumc = val.org.orgName
      this.s_formDatas.chengShi = val.org.city
      this.s_formDatas.fuZerxm = val.org.orgBossName
      for (let item of this.s_FormInfo) {
        if (item.name === 'keHushxxdm'
           ||item.name === 'keHudm'
           ||item.name === 'keHumc'
           ||item.name === 'chengShi')  {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      }
    },
  },
}
</script>

<style>
</style>